<template>
  <div class="menu_box">
    <h5 class="menu_title">
      <img class="icons" src="../../public/LogosApiary.png" />
      <span>vue3后台管理系统</span>
    </h5>
    <el-menu
      :default-active="$route.path"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      router
    >
      <el-menu-item :index="item.path" v-for="(item, index) in filteredMenus" :key="item.id">
        <component class="icons" :is="item.icon"></component>
        <span>{{ item.name }}</span>
      </el-menu-item>
    </el-menu>
  </div>
</template>

<script lang="ts" setup>
import { computed } from 'vue'
import { useUserStore } from '@/stores/permission'
import menuList from '@/json/menu.json'
const userStore = useUserStore()
const userPermissions = JSON.parse(userStore.userInfo as any)

const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

// 计算属性：过滤后的菜单
const filteredMenus = computed(() => {
  const userPermissionsList = userPermissions.permissions
  let menus = menuList.menus.filter((item) => {
    if (item.permission === null) return true
    // 过滤出当前用户权限包含的菜单
    return userPermissionsList.includes(item.permission)
  })
  return menus
})
</script>
<style lang="scss" scoped>
.menu_box {
  width: 100%;
  height: 100%;
  background-color: #fff;
  .menu_title {
    display: flex;
    align-items: center;
    text-align: left;
    line-height: 20px;
    font-size: 16px;
    color: #606266;
    padding: 10px 15px;
    margin: 0;
    border-bottom: 1px solid #dcdfe6;
  }
}

.icons {
  width: 20px;
  height: 20px;
  margin-right: 5px;
}
</style>
